var instanceId = T.p("instanceId");
var workItemId = T.p("workItemId");
var workItemDtlId = T.p("workItemDtlId");
var subjectId = T.p("subjectId");
var isShowCond = T.p("isShowCond");
var logStatusList = T.getDictItems("skd_log_status");
$(function () {
    $("#jqGrid").jqGrid({
        url: '../scheduleLog/list',
        datatype: "json",
        postData: {instanceId: instanceId, workItemId: workItemId, workItemDtlId: workItemDtlId},
        colModel: [
            {label: 'id', name: 'id', width: 50, key: true},
            {label: '实例Id', name: 'instanceId', index: 'instance_id', width: 80},
            {label: '实例明细Id', name: 'instanceDtlId', index: 'instance_dtl_id', width: 80},
            {label: '作业Id', name: 'workId', index: 'work_id', width: 80},
            {label: '作业名称', name: 'workName', index: 'work_name', width: 80},
            {label: '作业项id', name: 'workItemId', index: 'work_item_id', width: 80},
            {label: '作业项名称', name: 'workItemName', index: 'work_item_name', width: 80},
            {label: '处理数', name: 'dealCnt', index: 'deal_cnt', width: 80},
            {
                label: '状态', name: 'status', width: 50, formatter: function (value, options, row) {
                    var show = '';
                    if (value === 1) {
                        show = '<span class="label label-info">进行中</span>';
                    } else if (value === 3) {
                        show = '<span class="label label-success">成功</span>';
                    } else {
                        show = '<span class="label label-danger pointer" onclick="vm.showError(' + row.id + ')">失败</span>';
                    }
                    return show;
                }
            },
            {label: '备注', name: 'remark', index: 'remark', width: 80},
            {label: 'error', name: 'error', width: 80, hidden: true},
            {label: '开始时间', name: 'startTime', index: 'start_time', width: 80},
            {label: '结束时间', name: 'endTime', index: 'end_time', width: 80}
        ],
        viewrecords: true,
        height: 800,
        rowNum: 20,
        rowList: [20, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.data",
            page: "page.pageNo",
            total: "page.pageCount",
            records: "page.count"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            //$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        isShowCond: true,
        btnText: '隐藏',
        qryForm: {
            instanceId: instanceId,
            workItemId: workItemId,
            workItemDtlId: workItemDtlId,
            status: status
        }
    },
    methods: {
        reset: function () {

        },
        back: function (event) {
            if (workItemId != null) {
                location.href = "/work/work.html?subjectId=" + subjectId + "&isShowCond=" + isShowCond;
            } else if (instanceId != null) {
                location.href = "/skd/scheduleInstance.html";
            } else {
                history.go(-1);
            }

        },
        query: function () {
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    instanceId: vm.qryForm.instanceId,
                    workItemId: vm.qryForm.workItemId,
                    workItemDtlId: vm.qryForm.workItemDtlId
                },
                page: 1
            }).trigger("reloadGrid");
        },
        toggle: function () {
            this.isShowCond = !this.isShowCond;

            if (this.isShowCond == true) {
                this.btnText = "隐藏"
            } else if (this.isShowCond == false) {
                this.btnText = "查询"
            }

        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }

            location.href = "scheduleLog_add.html?id=" + id;
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: "../scheduleLog/delete",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        showError: function (logId) {
            $.get("../scheduleLog/info/" + logId, function (r) {
                parent.layer.open({
                    type: 1,
                    title: '失败信息',
                    closeBtn: 1,
                    content: r.scheduleLog.error,
                    area: ['1000px', '800px'],
                    btn: '确认'
                });
            });
        }, changeStatus: function () {
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'status': vm.qryForm.status},
                page: 1
            }).trigger("reloadGrid");
        }
    }
});

setInterval(function () {
    $("#jqGrid").trigger("reloadGrid");
}, 10 * 1000);